//
//  AppDelegate.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/6.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       
        //现有 VM 单例对象
        QorumLogs.enabled = true
        QorumLogs.minimumLogLevelShown = 1
        
        QL2("UserAccount.sharedViewModel.account:\n  \(UserAccountViewModel.sharedUserAccount.account)")
        
        //设置全局外观
        setupAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.blueColor()
        window?.rootViewController = defaultRootViewController
        window?.makeKeyAndVisible()
        
        /// 响应通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootControllerNotification,
            object: nil,
            queue: nil) {[weak self] (notification) -> Void in
                
            let vc = notification.object != nil ? WelcomeViewController() : MainViewController()
                
                self?.window?.rootViewController = vc
        }
        
        return true
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: WBSwitchRootControllerNotification, object: nil)
        print("notificationCenter 88")
    }
    
/// 设置全局外观对象
    private func setupAppearance() {
        UINavigationBar.appearance().tintColor = WBTintColor
        UITabBar.appearance().tintColor = WBTintColor
    }
}



// MARK: - 界面切换
extension AppDelegate {
    
    private var defaultRootViewController: UIViewController {
        
        if isNewVersion {
            return NewfeatureViewController()
        }
        
        if UserAccountViewModel.sharedUserAccount.userLogon {
            return WelcomeViewController()
        } else {
            return MainViewController()
        }
    }
    
/// 判断版本
    private var isNewVersion: Bool {
        
     /// 字符串 获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        QL2("当前版本" + currentVersion)
        
    /// 获取之前版本 沙盒
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        QL2("沙盒版本" + (sandboxVersion ?? "无"))
        
        /// 保存版本信息
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
        
        //比较版本
       return !(currentVersion == sandboxVersion)
    }
}








